#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// NeatlyFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion

using System.Runtime.InteropServices;
using UnityEngine;

namespace Neatly.Native
{
    public class NeatlyNative : ManagerSingleton<NeatlyNative>
    {
#if UNITY_EDITOR
        static string UnityNativeMethod(string json)
        {
            NDebug.Log("native:{0}",json);
            return "{\"result\":0, \"code\":0}";
        }
        static int GetVersion()
        {
            return 1;
        }
#elif UNITY_ANDROID
        static string UnityNativeMethod(string json)
        {
            AndroidJavaClass jc = new AndroidJavaClass("com.prims.racing.UnityPlayerActivity");
            AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
            return jo.Call<string>("UnityNativeMethod", json);
        }
        
        static int GetVersion()
        {
            AndroidJavaClass contextCls = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            AndroidJavaObject context = contextCls.GetStatic<AndroidJavaObject>("currentActivity");
            AndroidJavaObject packageMngr = context.Call<AndroidJavaObject>("getPackageManager");
            string packageName = context.Call<string>("getPackageName");
            AndroidJavaObject packageInfo =
                packageMngr.Call<AndroidJavaObject>("getPackageInfo", packageName, 0);
            return packageInfo.Get<int>("versionCode");
        }
#elif UNITY_IOS
	    [DllImport("__Internal")]
	    private static extern string UnityNativeMethod(string json);
        [DllImport("__Internal")]
        private static extern int GetVersion();
#else
        static string UnityNativeMethod(string json)
        {
            return "{\"result\":0, \"code\":0}";
        }
        static int GetVersion()
        {
            return 1;
        }
#endif
        public static string CallNative(string json)
        {
            return UnityNativeMethod(json);
        }
        public static int GetVersionCode()
        {
            return GetVersion();
        }
    }
}
